﻿using RTW.DAL;
using RTW.OA.Models;
using System.Collections.Generic;
using System.Linq;
using RTW.Utils.Common;

namespace RTW.OA.Areas.System.Servers
{
    public class PositionServers
    {
        private readonly IRepository<position> PositionRepository;
        private readonly IRepository<department> DeptRepository;
        private readonly IRepository<user> UserRepository;
        public PositionServers()
        {
            PositionRepository = new DapperRepository<position>();
            DeptRepository = new DapperRepository<department>();
            UserRepository = new DapperRepository<user>();
        }
        internal void Add(position model)
        {
            PositionRepository.Insert(model);
        }

        internal void Update(position model)
        {
            PositionRepository.Update(model);
        }

        internal void Del(string id)
        {
            PositionRepository.DeleteByIds(id);
        }

        public List<position> GetPositions()
        {
            return PositionRepository.GetAllEnumerable().ToList();
        }

        public position GetPositionById(string id)
        {
            return PositionRepository.GetById(id);
        }

        public List<department> GetDepts()
        {
            List<department> model = CacheHelper.Get<List<department>>(DTKeys.CACHE_DEPARTMENT);
            if (model == null)
            {
                CacheHelper.Insert(DTKeys.CACHE_DEPARTMENT, DeptRepository.GetAllEnumerable().ToList());
                model = CacheHelper.Get<List<department>>(DTKeys.CACHE_DEPARTMENT);
            }
            return model;
        }

        public List<user> GetUsers()
        {
            List<user> model = CacheHelper.Get<List<user>>(DTKeys.CACHE_USER);
            if (model == null)
            {
                CacheHelper.Insert(DTKeys.CACHE_USER, UserRepository.GetAllEnumerable().ToList());
                model = CacheHelper.Get<List<user>>(DTKeys.CACHE_USER);
            }
            return model;
        }
    }
}